package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.Permission;
import com.sskj.api.domain.RolePermission;
import com.sskj.api.exception.LogicException;
import com.sskj.api.mapper.PermissionMapper;
import com.sskj.api.service.IPermissionService;
import com.sskj.common.dto.TreeNode;
import com.sskj.common.util.StrUtil;
import com.sskj.common.util.TreeNodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class PermissionService extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Override
    public void add(Permission permission) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Permission::getPid, permission.getPid())
                .eq(Permission::getName, permission.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("权限名称已存在");
        }

        permission.setCreateTime(LocalDateTime.now());
        save(permission);
    }

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Permission::getPid, id);
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("存在子节点，中止操作");
        }

        removeById(id);
    }

    @Override
    public void update(Permission permission) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(Permission::getId, permission.getId())
                .eq(Permission::getPid, permission.getPid())
                .eq(Permission::getName, permission.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("权限名称已存在");
        }

        permission.setUpdateTime(LocalDateTime.now());
        updateById(permission);
    }

    @Override
    public List<TreeNode<Permission>> tree(Integer status) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Permission::getSortOrder)
                .orderByDesc(Permission::getId);
        if (status != null && status >= 0) {
            lambdaQueryWrapper.eq(Permission::getStatus, status);
        }

        List<Permission> permissions = list(lambdaQueryWrapper);

        return buildTree(permissions);
    }

    @Override
    public List<TreeNode<Permission>> tree(Collection<Long> ids, Integer status) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Permission::getSortOrder);
        if (status != null && status >= 0) {
            lambdaQueryWrapper.eq(Permission::getStatus, status);
        }
        List<Permission> list = list(lambdaQueryWrapper);
        return buildTree(list);
    }

    @Override
    public List<Permission> allAction() {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Permission> list = list(lambdaQueryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.stream().filter(item -> StrUtil.isNotBlank(item.getAction())).toList();
        }
        return List.of();
    }

    @Override
    public List<Permission> listAction(Collection<Long> ids) {
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Permission::getId, ids);
        List<Permission> list = list(lambdaQueryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.stream().filter(item -> StrUtil.isNotBlank(item.getAction())).toList();
        }
        return List.of();
    }

    private List<TreeNode<Permission>> buildTree(List<Permission> permissions) {
        if (permissions != null && !permissions.isEmpty()) {
            List<TreeNode<Permission>> nodes = permissions.stream().map(item -> {
                TreeNode<Permission> treeNode = new TreeNode<>();
                treeNode.setId(item.getId());
                treeNode.setPid(item.getPid());
                treeNode.setName(item.getName());
                treeNode.setSource(item);

                return treeNode;
            }).toList();

            return TreeNodeUtil.buildTree(nodes, 0L);
        }

        return null;
    }
}
